﻿Imports System.IO
Imports System.Reflection
Imports XCLI_API
Imports System.Xml
Public Class Manifest_Control


    Public Shared Function Read_File(ByVal keyword As String) As String
        Try

            '
            ' Verify the configuration file exists.
            '
            Dim ManifestFilePath As String = Environment.CurrentDirectory & "\plugins\manifest.xml"
            Dim data As String = String.Empty
            If Not System.IO.File.Exists(ManifestFilePath) Then
                Manifest_Control.create()
            End If
            '
            ' Read the data
            '
            Dim xmlDoc As New Xml.XmlDocument
            Dim xmlNode As Xml.XmlNode
            Dim xmlNodeR As Xml.XmlNode
            Dim xmlFound As Boolean = False
            xmlDoc.Load(ManifestFilePath)
            xmlNode = xmlDoc.DocumentElement.SelectSingleNode("Plugins")
            xmlNodeR = xmlNode.SelectSingleNode(keyword.ToLower)
            data = xmlNodeR.InnerText
            If data <> "" Then
                Return data
            End If

        Catch ex As Exception
            Return "0"
        End Try
    End Function

    Public Shared Sub create()
        Dim ManifestFilePath As String = Environment.CurrentDirectory & "\plugins\manifest.xml"
        Try
            Dim commands As String = "cd,exit"
            Dim commandArr() As String = commands.Split(",")
            'If Not File.Exists(ManifestFilePath) Then
            '    File.Create(ManifestFilePath)
            'End If
            Dim writer As New XmlTextWriter(ManifestFilePath, System.Text.Encoding.UTF8)
            writer.WriteStartDocument(True)
            writer.Formatting = Formatting.Indented
            writer.Indentation = 2
            writer.WriteStartElement("root")
            writer.WriteStartElement("Plugins")

            'This is a single block for a keyword -> plugin entry
            'This is used only for default commands, and writes out any commands in the commandArr() array to the file
            For Each com In commandArr
                writer.WriteStartElement(com)
                writer.WriteString("xcli.core.dll")
                writer.WriteEndElement()
            Next

            'Write in plugin DLL-------------------
            writer.WriteStartElement("plugin")
            writer.WriteString("xcli.core.plugin.dll")
            writer.WriteEndElement()
            ''-------------------------------------
            writer.WriteEndElement()
            writer.WriteEndElement()
            writer.WriteEndDocument()
            writer.Close()


            Console.Write("XCLI was unable to find the manifest.xml file and has re-created it using the " & vbCrLf & "default settings. This may have lost any installed plugins." & vbCrLf & "Do you want to initiate the rebuild task?" & vbCrLf & "This will re-install every plugin in the ""Plugins"" directory (Y/N): ")
            Dim answer As String = Console.ReadLine
            If answer.ToUpper = "Y" Then
                If File.Exists(env(1) & "\xcli.core.plugin.dll") Then

                    Dim asm As Assembly = Assembly.LoadFrom(env(1) & "\xcli.core.plugin.dll")
                    Dim myType As System.Type = asm.GetType(asm.GetName.Name + ".Plugin")
                    Dim implementsXCLIPlugin As Boolean = GetType(XCLIPlugin).IsAssignableFrom(myType)
                    If implementsXCLIPlugin Then
                        'Console.WriteLine(asm.GetName.Name + " is a valid plugin!")
                        Dim plugin As XCLIPlugin = CType(Activator.CreateInstance(myType), XCLIPlugin)
                        'Console.WriteLine(plugin.CommonName & plugin.Description)
                        plugin.CONSTRUCT("plugin rebuild", env)
                    End If
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

End Class
